package it.univpm.diiga.memorysim.frames.process;

import it.univpm.diiga.memorysim.data.DataPlugin;
import it.univpm.diiga.memorysim.kernel.shared.ProcessRequest;

import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;

import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;




public class Detail extends ApplicationWindow {
	private Group group;
	private Table table;
	private int p_sel=0;
	
  public Detail(int p_sel) {
    super(null);
    this.p_sel=p_sel;
  }

  public void run() {
    setBlockOnOpen(true);
    open();
  }

  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    shell.setText("Dettagli processo - PID: "+p_sel);
  }

  /**
   * Creates the main window's contents
   * 
   * @param parent the main window
   * @return Control
   */
  protected Control createContents(Composite parent) {
	GridData gridData = new GridData();
	gridData.verticalSpan = 7;
	gridData.verticalAlignment = GridData.FILL;
	gridData.widthHint = 350;
	gridData.heightHint = 216;
	GridData gridData1 = new GridData();
	gridData1.verticalSpan = 7;
	gridData1.verticalAlignment = GridData.FILL;
	gridData1.widthHint = 400;
	gridData1.heightHint = 260;
    Composite composite = new Composite(parent, SWT.NONE);
    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 1;
    composite.setLayout(gridLayout);
    GridLayout gridLayout1 = new GridLayout();
	gridLayout1.numColumns = 1;
	group = new Group(composite, SWT.NONE);
	group.setLayoutData(gridData1);
	group.setLayout(gridLayout1);
    table = new Table(group, SWT.NONE|SWT.BORDER);
    table.setLayoutData(gridData);
    table.setHeaderVisible(true);
    String[] titles_paginazione = { "Pagina","Offset","Frame"};
    String[] titles_segmentazione = { "Segmento", "Offset", "Base", "Limite" };
    String[] titles;
	if(DataPlugin.SimSelected.equals("Segmentazione")) titles=titles_segmentazione;
	else titles=titles_paginazione;
	
    for (int loopIndex=0; loopIndex<titles.length; loopIndex++) {
    	TableColumn column = new TableColumn(table, SWT.NULL);
        column.setText(titles[loopIndex]);
        column.setResizable(true);
        
    }
    for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
         table.getColumn(loopIndex).pack();
    }
    
	if(DataPlugin.SimSelected.equals("Segmentazione")){
		group.setText("Segmenti richiesti del processo PID:"+p_sel);
		int n=1;
		 for(int i=0;i<DataPlugin.listProcessRequest.size();i++){
		    	ProcessRequest dato = DataPlugin.listProcessRequest.get(i);
		    	if(dato.getPID()==p_sel){
			    	TableItem itemRow=new TableItem(table,SWT.NONE);
			    	itemRow.setText(0, dato.getSegmento()+""); 
			    	itemRow.setText(1,dato.getOffset()+"");
			    	itemRow.setText(2,dato.getStart()+"");
					itemRow.setText(3,dato.getDimBlock()+"");
				}
		    }
	}
	else{
		group.setText("Pagine richieste del processo PID:"+p_sel);
		for(int i=0;i<DataPlugin.listProcessRequest.size();i++){
	    	ProcessRequest dato = DataPlugin.listProcessRequest.get(i);
	    	if(dato.getPID()==p_sel){
	    		TableItem itemRow=new TableItem(table,SWT.NONE);
		    	itemRow.setText(0,dato.getPagina()+"");
				itemRow.setText(1, dato.getOffset()+"");
				itemRow.setText(2, dato.getStart()+"");
	    	}
    	}
	}

    parent.pack();
    return composite;
  }
  
}	
